package com.yinhe.music.yhmusic.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yinhe.music.yhmusic.R;

import java.util.List;
import java.util.Map;

/**
 * Created by hasee on 2017/2/11.
 */

public class ListViewAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater = null;
    private List<Map<String, Object>> data;
    public ListViewAdapter(Context mContext, List<Map<String, Object>> data){
        this.mInflater = LayoutInflater.from(mContext);
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
       final viewHolder  holder;
        //如果缓存convertView为空，则需要创建View
        if (view == null) {
            holder = new viewHolder();
            //根据自定义的Item布局加载布局
            view = mInflater.inflate(R.layout.listview_item, null);
            holder.img = (ImageView) view.findViewById(R.id.quality_img);
            holder.tx1 = (TextView) view.findViewById(R.id.quality_name);
            holder.tx2 = (TextView) view.findViewById(R.id.quality_size);
            view.setTag(holder);
        }else {
            holder = (viewHolder) view.getTag();
        }
        holder.img.setImageResource((Integer) data.get(i).get("img"));
        holder.tx1.setText((String) data.get(i).get("name"));
        holder.tx2.setText((String)data.get(i).get("size"));
        return view;
    }

    static class viewHolder{
        private ImageView img;
        private TextView tx1,tx2;
    }
}
